<?php

require_once dirname(__FILE__) . '/../users/UsersView.php';

/**
* Varje funktion i klassen returnerar ett ValidationResult-objekt, som innehåller det
* kontrollerade värdet, eller en bit-flagga vid fel.
*/
class Validate {
	const EMAIL_EMPTY = 1;
	const EMAIL_INVALID = 2;
	const USERNAME_EMPTY = 4;
	const USERNAME_INVALID = 8;
	const NAME_EMPTY = 16;
	const NAME_INVALID = 32;
	const PASSWORD_EMPTY = 64;
	const PASSWORD_INVALID = 128;
	const HOUSE_INVALID = 256;
	const USER_ROLE_INVALID = 512;

	static public function email($email) {
		$email = trim($email);

		if (!$email) {
			return ValidationResult::createInvalid(self::EMAIL_EMPTY);
		}

		$result = filter_var($email, FILTER_VALIDATE_EMAIL);

		if ($result == FALSE) {
			return ValidationResult::createInvalid(self::EMAIL_INVALID);
		}

		return ValidationResult::createValid($result);
	}

	static public function username($username) {
		$username = trim($username);

		if (!$username) {
			return ValidationResult::createInvalid(self::USERNAME_EMPTY);
		}

		$result = filter_var($username, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^[a-zA-Z0-9_]{3,30}$/")));

		if ($result == FALSE) {
			return ValidationResult::createInvalid(self::USERNAME_INVALID);
		}

		return ValidationResult::createValid($result);
	}

	static public function name($name) {

		$name = trim($name);
		$name = strip_tags($name);

		if (!$name) {
			return ValidationResult::createInvalid(self::NAME_EMPTY);
		}

		$result = filter_var($name, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^.{2,30}$/")));

		if ($result == FALSE) {
			return ValidationResult::createInvalid(self::NAME_INVALID);
		}

		return ValidationResult::createValid($result);
	}

	static public function password($password) {

		$password = trim($password);

		if (!$password) {
			return ValidationResult::createInvalid(self::PASSWORD_EMPTY);
		}

		$result = filter_var($password, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^.{8,100}$/")));

		if ($result == FALSE) {
			return ValidationResult::createInvalid(self::PASSWORD_INVALID);
		}

		return ValidationResult::createValid($result);
	}

	static public function house($house) {
		if (array_search($house, UsersView::houses) === false) {
			return ValidationResult::createInvalid(self::HOUSE_INVALID);
		}

		return ValidationResult::createValid($result);
	}

	static public function userRole($userRole) {
		$usersView = new UsersView();

		if (isste($usersView->roles[(string)$userRole])) {
			return ValidationResult::createInvalid(self::USER_ROLE_INVALID);
		}

		return ValidationResult::createValid($result);
	}

	static public function dateValidator($date) {

		$trimDate = trim($date);
		
	/*	if (!is_numeric($trimDate)) {
			echo "Inte bara nummer<br>";
			return new ValidationResult(NULL, false);
		}
		*/
		
		
		$length = strlen($date);
		//	if($length == 8 )

		$HyphenOne = substr($trimDate, -3, 1);

		$HyphenTwo = substr($trimDate, -6, 1);

		echo $HyphenOne . $HyphenTwo;

		if ($HyphenOne == "-" && $HyphenTwo == "-") {

			$hyphenStrippedDate = str_replace("-", "", $trimDate);
			
		} 
		else {
			echo "bindestrecket finns inte på rätt ställen";
			return new ValidationResult(NULL, false);
		}
		

		if (strlen($hyphenStrippedDate) == 6 || strlen($hyphenStrippedDate) == 8) {
					echo substr($hyphenStrippedDate, -4);
			$this->datePart(substr($hyphenStrippedDate, -4));
				
		}
		
		else {

			echo "Fel langd<br>";
			return new ValidationResult(NULL, false);
		}

		echo "yeah : " . $hyphenStrippedDate;
		return true;

	}

	static private function datePart($date) {

		$month = substr($date, 0, 2);
		$day = substr($date, 2);
		$dayPerMonthArray = array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

		if ($month >= 1 && $month <= 12) {

			if (!($day >= 1 && $day <= $dayPerMonthArray[$month - 1])) {
				return FALSE;
			}
		} else {
			return FALSE;
		}
		return TRUE;
	}

}

class ValidationResult {

	private $value;
	private $errorCode;
	
	public function getValue() {
		return $this->value;
	}
	
	public function isValid() {
		return ($this->errorCode === 0);
	}

	public function getErrorFlag() {
		return $this->errorCode;
	}

	private function __construct() {}

	public static function createValid($value) {
		$self = new ValidationResult();
		$self->value = $value;
		$self->errorCode = 0;

		return $self;
	}

	public static function createInvalid($errorCode) {
		$self = new ValidationResult();
		$self->value = null;
		$self->errorCode = $errorCode;

		return $self;
	}
}
